package ma.jeetconsulting.pocs.service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TXTReporter implements Reporter {
	
	Configuration cfg = new Configuration();
	
	@Override
	public void makeReport(List<ModuleReport> moduleReports) throws IOException, TemplateException {
		for (ModuleReport moduleReport : moduleReports) {
			makeReport(moduleReport);
		}
	}
	
	private void makeReport (ModuleReport moduleReport) throws IOException, TemplateException{
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("classesStoredByPackage", moduleReport.getClassesSortedByPackages());
		data.put("packagesNumber", moduleReport.getNumberOfPackages());
		data.put("classesNumber", moduleReport.getNumberOfClasses());
		Template template = cfg.getTemplate("src/main/resources/report.ftl");
		Writer reportFile = new FileWriter(new File("fileReportes" + moduleReport.getModuleName() +".txt" ));
		template.process(data, reportFile);
		
		reportFile.flush();
		reportFile.close();
	}

}
